package com.centling.smartSealForPhone.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.ApplySealActivity;
import com.centling.smartSealForPhone.bean.HistoryListBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.view.CircleImageView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author fionera
 */
public class HistoryListAdapter
        extends RecyclerView.Adapter {
    private Context context;
    private List<HistoryListBean.RowsBean> list;

    public HistoryListAdapter(Context context, List<HistoryListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HistoryViewHolder(
                LayoutInflater.from(context).inflate(R.layout.rv_history_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final HistoryViewHolder viewHolder = (HistoryViewHolder) holder;

        ImageUtil.loadAvatarImage(list.get(position).getApplicantUserAvatar(),
                viewHolder.avatar, R.drawable.iv_avatar_default);

        viewHolder.user.setText(list.get(position).getApplicantUserName());
        viewHolder.time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
                .format(new Date(list.get(position).getCreatedDate())));

        viewHolder.tvReasonTitle.setText(TextUtils.equals(list.get(position).getApplyType(), "COUNT_LIMIT") ? String
                .format(Locale.CHINA, "可用次数:%d/%d次", list.get(position).getRemainTimes(),
                        list.get(position).getApplyTimes()) : String
                .format(Locale.CHINA, "%s至%s", list.get(position).getApplyStartTimeReal().split(" ")[0],
                        list.get(position).getApplyEndTimeReal()).split(" ")[0]);
        viewHolder.tvReason.setText(list.get(position).getApplyReasonType());
        /*
           0审核中， 1使用中，2审核失败  3未审核过期 4审核成功后过期 5用印完成
           6复审成功 7复审失败  8补拍完成 9申请驳回
         */
        String state = list.get(position).getStatus();
        switch (state) {
            case "":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_0));
                viewHolder.state.setText("审批中");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "0":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_0));
                viewHolder.state.setText("审批中");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "1":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_1));
                viewHolder.state.setText("用印中");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "2":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_2));
                viewHolder.state.setText("审批失败");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "3":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_3));
                viewHolder.state.setText("未审批\n过期");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "4":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_4));
                viewHolder.state.setText("审批后\n过期");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "5":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_5));
                viewHolder.state.setText("用章完成");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "6":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_6));
                viewHolder.state.setText("复审成功");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "7":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_7));
                viewHolder.state.setText("复审失败");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "8":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_8));
                viewHolder.state.setText("补拍完成");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            case "9":
                if(TextUtils.equals(list.get(position).getRejectEditable(),"0")) {
                    viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_9));
                    viewHolder.state.setText("申请驳回");
                    viewHolder.state.setBackground(null);
                    viewHolder.state.setClickable(false);
                }else{
                    viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.blue1));
                    viewHolder.state.setText("编辑提交");
                    viewHolder.state.setBackgroundResource(R.drawable.sl_bg_white_blue1_white_blue2_corner4);
                    viewHolder.state.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            context.startActivity(new Intent(context, ApplySealActivity.class).putExtra("sealApplyId",list.get(position).getSealApplyId()));
                        }
                    });
                }
                break;
            case "10":
                viewHolder.state.setTextColor(ContextCompat.getColor(context, R.color.history_state_10));
                viewHolder.state.setText("驳回过期");
                viewHolder.state.setBackground(null);
                viewHolder.state.setClickable(false);
                break;
            default:
                break;
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private class HistoryViewHolder
            extends RecyclerView.ViewHolder {
        private CircleImageView avatar;
        private TextView user;
        private TextView time;
        private TextView state;
        private TextView tvReasonTitle;
        private TextView tvReason;

        HistoryViewHolder(View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.civ_history_list_preview);
            user = itemView.findViewById(R.id.tv_history_list_name);
            tvReasonTitle = itemView.findViewById(R.id.tv_history_list_reason_title);
            tvReason = itemView.findViewById(R.id.tv_history_list_reason);
            time = itemView.findViewById(R.id.tv_history_list_time);
            state = itemView.findViewById(R.id.tv_history_list_state);
        }
    }

    public interface OnClickListener {
        void onClick(View v, int pos);
    }

    private OnClickListener mOnClickListener;

    public void setOnClickListener(OnClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }
}
